package homework02;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {
	static List<Student> listOfStudent = new ArrayList<Student>();
	static List<Score> listOfScore = new ArrayList<Score>();
	static Scanner input = new Scanner(System.in);

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		File file1 = new File(
				"C:\\Users\\小苏\\git\\Java_KaiFaJiChu_assignment\\DevelopmentBasic\\文档\\data\\Student.txt");
		try {
			Reader in1 = new FileReader(file1);
			BufferedReader bufferRead1 = new BufferedReader(in1);
			String str1 = null;
			while ((str1 = bufferRead1.readLine()) != null) {
				String[] ss = str1.split("\\s+");
				Student stu_1 = new Student(ss[0], ss[1], ss[2], ss[3]);
				listOfStudent.add(stu_1);
			}
			bufferRead1.close();
		} catch (IOException e1) {
			System.out.println(e1.toString());
		}

		// for (Student student : listOfStudent) {
		// System.out.println(student.toString());
		// }

		File file2 = new File("C:\\Users\\小苏\\git\\Java_KaiFaJiChu_assignment\\DevelopmentBasic\\文档\\data\\Score.txt");
		try {
			Reader in2 = new FileReader(file2);
			BufferedReader bufferRead2 = new BufferedReader(in2);
			String str2 = null;
			while ((str2 = bufferRead2.readLine()) != null) {
				String[] ss2 = str2.split("\\s+");
				Score score = new Score(ss2[0], ss2[1], Integer.parseInt(ss2[2]));
				listOfScore.add(score);
			}
			bufferRead2.close();
		} catch (IOException e1) {
			System.out.println(e1.toString());
		}
		// for (Score score : listOfScore) {
		// System.out.println(score.toString());
		// }
		while (true) {
			System.out.println("1.查询某门课程的所有学生的平均成绩;");
			System.out.println("2.查询某门课程的最高成绩");
			System.out.println("3.查询某门课程的最低成绩");
			System.out.println("4.查询某个学生的所有成绩");
			System.out.println("请输入你的选择：");
			int choice = input.nextInt();
			if (choice >= 0 && choice <= 4) {
				switch (choice) {
				case 1:
					getAvgScore();
					break;
				case 2:
					getMaxScore();
					break;
				case 3:
					getMinScore();
					break;
				case 4:
					getAllScore();
					break;
				case 0:
					return;
				}
				System.out.println("\n");
			} else {
				System.out.println("输入不合法，请重新输入！");
			}
		}
	}

	private static void getAvgScore() {
		System.out.println("请输入你想要查看的学生的平均成绩的课程名称：");
		String nameOfCourse = input.next();
		int sum = 0;
		int count = 0;
		for (Score score : listOfScore) {
			if (score.getNameOfCourse().equals(nameOfCourse)) {
				sum += score.getMark();
				count++;
			}
		}
		System.out.println("总分是：" + sum);
		System.out.println("总人数是：" + count);
		System.out.println("平均分是：" + (double) sum / count);

	}

	private static void getMaxScore() {
		System.out.println("请输入你想要查看的最高成绩的课程名称：");
		String nameOfCourse = input.next();
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (Score score : listOfScore) {
			if (score.getNameOfCourse().equals(nameOfCourse)) {
				list.add(score.getMark());
			}
		}
		int max = list.get(0);
		for (int i = 1; i < list.size(); i++) {
			if (max < list.get(i)) {
				max = list.get(i);
			}
		}
		System.out.println(max);
	}

	private static void getMinScore() {
		System.out.println("请输入你想要查看的最低成绩的课程名称：");
		String nameOfCourse = input.next();
		ArrayList<Integer> list = new ArrayList<Integer>();
		for (Score score : listOfScore) {
			if (score.getNameOfCourse().equals(nameOfCourse)) {
				list.add(score.getMark());
			}
		}
		int min = list.get(0);
		for (int i = 1; i < list.size(); i++) {
			if (min > list.get(i)) {
				min = list.get(i);
			}
		}
		System.out.println(min);
	}

	private static void getAllScore() {
		System.out.println("请输入你想要查看的所有成绩的学生学号：");
		String strId = input.next();
		// List<Score> listOfScore2 = new ArrayList<Score>();
		for (Score score : listOfScore) {
			if (score.getId().equals(strId)) {
				// listOfScore2.add(score);
				System.out.println(score.toString());
			}
		}
	}
}
